﻿using System;
using System.Management.Automation;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;

namespace ExcelPoSH
{
    
    // Cmdlet to allow user to select an excel range object
    //
    // Output: The selected range or null
    
    [Cmdlet ("Read", "ExcelRange")]
    public class ReadExcelRangeCommand : Cmdlet
    {
        private string mTitle = "Select range";
        private string mPrompt = "Select a range of cells";
        private uint mRangeCount = 1;

        [Parameter(Mandatory = false, Position = 3)]
        public uint RangeCount
        {
            get
            {
                return mRangeCount;
            }
            set
            {
                mRangeCount = value;
            }
        }

        [Parameter(Mandatory = false, Position = 2)]
        public string Title
        {
            get
            {
                return mTitle;
            }
            set
            {
                mTitle = value;
            }
        }

        [Parameter(Mandatory = false, Position = 1)]
        public string Prompt
        {
            get
            {
                return mPrompt;
            }
            set
            {
                mPrompt = value;
            }
        }
        
        protected override void ProcessRecord()
        {
            try
            {
                Excel.Application app = Globals.ThisAddIn.Application;

                // Show Excel.Application.InputBox RangeCount times
                // and write the selected range or null to pipeline

                for (int i = 0; i < RangeCount; i++)
                {
                    object retVal = app.InputBox(Prompt, Title,
                        Missing.Value, Missing.Value, Missing.Value,
                        Missing.Value, Missing.Value, 8);

                    if (retVal is bool)
                    {
                        WriteObject(null);
                    }
                    else
                    {
                        WriteObject(retVal as Excel.Range);
                    }
                }
            }
            catch (Exception e)
            {
                ErrorRecord errorRecord = new ErrorRecord(
                    e, "Exception", ErrorCategory.ReadError, null);
                WriteError(errorRecord);
            }
        }
    }

    // Cmdlet to write values to Excel
    // For now no arrays, and write only to active cell
    //
    // Input: an object

    [Cmdlet("Write", "Excel")]
    public class WriteExcelCommand : Cmdlet
    {
        private Excel.Workbook mWorkbook;
        private Excel.Worksheet mWorksheet;
        private Excel.Range mStartingRange;
        private Object mValue = null;

        [Parameter(Mandatory = false, Position = 1,
            ValueFromPipeline = true)]
        public Object Value
        {
            get
            {
                return mValue;
            }
            set
            {
                mValue = value;
            }
        }

        // Write Value.ToString() to the active Excel cell

        protected override void ProcessRecord()
        {
            try
            {
                Excel.Application app = Globals.ThisAddIn.Application;

                if (app != null)
                {
                    if (app.ActiveCell != null)
                    {
                        if (Value != null)
                        {
                            app.ActiveCell.Value2 = Value.ToString();
                        }
                        ((Excel.Range)app.Cells[app.ActiveCell.Row + 1,
                            app.ActiveCell.Column]).Activate();
                    }
                }
            }
            catch (Exception e)
            {
                Globals.ThisAddIn.console.SetText(e.Message, true);
            }
        }
    }
}